package qmm.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;

import qmm.dao.AddressDao;
import qmm.dao.impl.AddressDaoImpl;
import qmm.entity.Address;
import qmm.entity.User;

@WebServlet("/AddressServlet")
public class AddressServlet extends BaseServlet{
	
	AddressDao dao = new AddressDaoImpl();
	
	public void getDefaultAddress(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		
		Address address = dao.getDefaultAddress(user.getId());
		session.setAttribute("defaultAddress", address);
		response.getWriter().print("success");
	}
	
	public void getList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		
		List<Address> list = dao.getList(user.getId());
		session.setAttribute("addressList", list);
		response.sendRedirect("address.jsp");
	}
	
	public void findById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		int id = Integer.parseInt(request.getParameter("id"));
		Address address = dao.findById(id);
		String json = JSON.toJSONString(address);
		response.getWriter().write(json);
	}
	
	public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		
		
		String address = request.getParameter("address");
		String remark = request.getParameter("remark");
		String receiver = request.getParameter("receiver");
		String phone = request.getParameter("phone");
		String email = request.getParameter("email");
		String part = request.getParameter("part");
		String flag = request.getParameter("flag");
		String receiveTime = request.getParameter("receiveTime");
		String province = request.getParameter("province");
		String city = request.getParameter("city");
		String district = request.getParameter("district");
		
		Address addr = new Address(user.getId(), address, remark, receiver, phone, email, part, flag, receiveTime, province, city, district);
		System.out.println(addr);
		
		int n = dao.add(addr);
		if(n>0){
			getList(request, response);
		}
	}
	
	public void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		
		int id = Integer.parseInt(request.getParameter("id"));
		String address = request.getParameter("address");
		String remark = request.getParameter("remark");
		String receiver = request.getParameter("receiver");
		String phone = request.getParameter("phone");
		String email = request.getParameter("email");
		String part = request.getParameter("part");
		String flag = request.getParameter("flag");
		String receiveTime = request.getParameter("receiveTime");
		String province = request.getParameter("province");
		String city = request.getParameter("city");
		String district = request.getParameter("district");
		
		Address addr = new Address(id, user.getId(), address, null, 0, remark, receiver, phone, email, part, flag, receiveTime, province, city, district);
		
		int n = dao.edit(addr);
		if(n>0){
			getList(request, response);
		}
	}
	
	public void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		int id = Integer.parseInt(request.getParameter("id"));
		int n = dao.del(id);
		if(n>0){
			getList(request, response);
		}
	}
	
	public void setDefault(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		int id = Integer.parseInt(request.getParameter("id"));
		//清空用户的默认收货地址
		dao.clearDefault(user.getId());
		//设置默认地址
		int n = dao.setDefault(id);
		if(n>0){
			getList(request, response);
		}
	}
	
	public void setAddress(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		int id = Integer.parseInt(request.getParameter("id"));
		Address address = dao.findById(id);
		session.setAttribute("defaultAddress", address);//替换session数据
		response.sendRedirect("cartwo.jsp");
	}
}
